package com.zh.obsdemo.test;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取临时aksk
 */
public class AKSK {
	public static JSONObject getAKSK() throws NoSuchAlgorithmException, KeyManagementException, IOException {
		String akUrl = "https://iam.cn-north-4.myhuaweicloud.com/v3.0/OS-CREDENTIAL/securitytokens";
		//HashMap<String, String> header = new HashMap<>();
		//header.put("Content-Type", "application/json");
		//header.put("X-Auth-Token",Authentication.getToken());
		HashMap<String, HashMap<String, Map<String, List>>> body = new HashMap<>();
		HashMap<String, Map<String, List>> auth = new HashMap<>();
		HashMap<String, List> identity = new HashMap<>();

		ArrayList<String> methods = new ArrayList<>();
		methods.add("token");
		identity.put("methods", methods);
		auth.put("identity", identity);
		body.put("auth", auth);
		System.out.println(JSON.toJSONString(body));
		HttpRequest request = HttpUtil.createRequest(Method.POST, akUrl);
		request.header("Content-Type", "application/json");
		request.header("X-Auth-Token", Authentication.getToken());
		HttpResponse httpResponse = request.body(JSON.toJSONString(body)).execute();
		//HttpUtils httpUtils = new HttpUtils();
		//StreamClosedHttpResponse httpResponse = httpUtils.doPost(akUrl, header, JSON.toJSONString(body));
		Map<String, List<String>> headers = httpResponse.headers();
		JSON.toJSONString(headers);
		return JSON.parseObject(httpResponse.body());
	}
}